package edu.fmi.st.epf.types.skill.ui.provider;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.epf.library.edit.category.TransientCategoryPackageItemProvider;
import org.eclipse.epf.uma.ContentCategory;
import org.eclipse.epf.uma.ContentPackage;
import org.eclipse.epf.uma.CustomCategory;
import org.eclipse.epf.uma.UmaPackage;
import org.eclipse.swt.graphics.Image;

import edu.fmi.st.epf.types.skill.common.util.SkillHelper;
import edu.fmi.st.epf.types.skill.ui.SkillActivator;

public class SkillsCategoryItemProvider extends
		TransientCategoryPackageItemProvider {

	/**
	 * Creates a new instance.
	 */
	public SkillsCategoryItemProvider(AdapterFactory adapterFactory,
			ContentPackage target, String name) {
		super(adapterFactory, target, name);
	}

	@Override
	protected boolean isInherited(ContentCategory category) {
		return false;
	}

	@Override
	protected void collectNewChildDescriptors(
			Collection<Object> newChildDescriptors, Object object) {
		newChildDescriptors.add(createChildParameter(
				UmaPackage.eINSTANCE.getContentPackage_ContentElements(),
				SkillHelper.makeCategory(SkillHelper.MAKE_SKILL_TEXT)));
		newChildDescriptors.add(createChildParameter(
				UmaPackage.eINSTANCE.getContentPackage_ContentElements(),
				SkillHelper.makeCategory(SkillHelper.MAKE_GROUP_TEXT)));
	}

	@Override
	public Collection<? extends EStructuralFeature> getChildrenFeatures(
			Object object) {
		if (childrenFeatures == null) {
			childrenFeatures = new ArrayList<EStructuralFeature>();
			childrenFeatures.add(UmaPackage.eINSTANCE
					.getContentPackage_ContentElements());
		}
		return childrenFeatures;
	}

	private static boolean accept(Object obj) {
		if (obj instanceof CustomCategory) {
			return true;
		}
		return false;
	}

	@Override
	protected boolean acceptAsChild(Object obj) {
		if (!super.acceptAsChild(obj)) {
			return false;
		}
		return accept(obj);
	}

	@Override
	public Image getImage(Object object) {
		return SkillActivator.getDefault().getImage("full/obj/Skills"); //$NON-NLS-1$
	}

	@Override
	public void setDefaultName(Object obj) {
		BaseSkillsItemProvider.setDefaultName(obj, target);
	}

	@Override
	public String getCreateChildText(Object owner, Object feature,
			Object child, Collection<?> selection) {
		return BaseSkillsItemProvider.getCreateChildText(child);	
	}

	@Override
	public Object getCreateChildImage(Object owner, Object feature,
		Object child, Collection<?> selection) {
		if (child instanceof CustomCategory) {
			return SkillActivator.getDefault().getImage("full/obj/Skill"); //$NON-NLS-1$
		}
		throw new IllegalStateException(
			"Unknown child type for Skill: " + child);
	}

}
